package thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;

public class TestThread extends Thread{


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("B");
                countDownLatch.countDown();
            }
        });
        Thread c = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    b.join();
                    System.out.println("C");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    // 一定要保证每个线程执行完毕或者异常后调用countDown()方法
                    // 如果不调用会导致其他线程一直等待, 无法继续执行
                    // 建议放在finally代码块中, 防止异常情况下未调用countDown()方法
                    countDownLatch.countDown();
                }
            }
            });
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    c.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    // 一定要保证每个线程执行完毕或者异常后调用countDown()方法
                    // 如果不调用会导致其他线程一直等待, 无法继续执行
                    // 建议放在finally代码块中, 防止异常情况下未调用countDown()方法
                    countDownLatch.countDown();
                    System.out.println("A");
                }
            }
        });
        a.start();
        b.start();
        c.start();
        countDownLatch.await();
        System.out.println("end");


        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("hhh");
            }
        });
    }




}
